package TUILayer;

import ControlLayer.*;
import ModelLayer.*;
import java.util.Scanner;
/**
 * Write a description of class LoanUI here.
 * 
 * @author (team 2) 
 * @version (v1)
 */
public class LoanUI
{
    private LoanCtr loanCtr;
    private AddressCtr addrCtr;
    private DVDCtr dvdCtr;

    //Constructor
    public LoanUI()
    {
        loanCtr = new LoanCtr();
        addrCtr = new AddressCtr();
        dvdCtr = new DVDCtr();
    }
    /**
     * Starts loan UI
       */
    public void start(LoanCtr ctr)
    {
        loanCtr = ctr;
        loanMenu();
    }
    /**
     * loan menu
       */
    public void loanMenu()
    {
        boolean exit = false;

        while(!exit)
        {
            int input = writeLoanMenu();
            if(input == 1)
            {
                createLoan();
            }else{
                if(input == 2){
                    //find loan
                    findLoan();
                }else{
                    if(input == 3)
                    {
                        //Delete loan
                        deleteLoan();
                    }else{
                        if(input == 4){
                            //List all loans
                            listAllLoans();
                        }else{
                            if(input == 5){
                                System.out.println("Going to main menu");
                                exit = true;
                            }else{
                                exit = true;
                            }
                        }
                    }
                }
            }
        }
    }
    /**
     * Returns a int as input for menu
       */
    private int writeLoanMenu()
    {
        Scanner key = new Scanner(System.in);
        System.out.println("*** Loan Menu ***");
        System.out.println("(1) Create loan ");
        System.out.println("(2) Find loan ");
        System.out.println("(3) Delete Loan ");
        System.out.println("(4) List all loans ");
        System.out.println("(5) Quit ");
        int choice = key.nextInt();
        return choice;
    }
    /**
     * Create a loan
       */
    public void createLoan()
    {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter loan id: ");
        long id = input.nextLong();
        System.out.println("Enter borrowed date: ");
        String bd = input.next();
        System.out.println("Enter period: ");
        String per = input.next();
        System.out.println("Enter state: ");
        String state = input.next();
        System.out.println("Enter copy serial: ");
        long copysn = input.nextLong();

        Copy c = dvdCtr.findCopy(copysn);
        
        //check if copy is equal to null
        if(c != null)
        {
            System.out.println("Enter Person Id: ");
            long perId = input.nextLong();        
            Person p = addrCtr.findPerson(perId);
            if(p == null)
            {
                System.out.println("No person found!");
            }
            else
            {
                loanCtr.createLoan(id, bd, per, state, copysn, perId);
            }  
        }
        else
        {
            System.out.println("No copy found!");
        }
    }
    /**
     * Deletes a loan
       */
    public void deleteLoan()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter loan id: ");
        long id = input.nextLong();
        System.out.println("Enter copy serial: ");
        long copySn = input.nextLong();

        Loan tempLoan = loanCtr.findLoan(id);
        Copy c = dvdCtr.findCopy(copySn);

        //Check if loan and copy is not equal to null
        if(tempLoan != null && c != null)
        {
            c.setIsLentOut(false);
            loanCtr.deleteLoan(id);
        }
        else
        {
            System.out.println("No loan has been found!");
        }
    }
    /**
     * Lists all loans
       */
    public void listAllLoans()
    {
        loanCtr.listAllLoans();
    }
    /**
     * Find a loan
       */
    public void findLoan()
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter load id");
        long id = input.nextLong();
        Loan tempLoan = loanCtr.findLoan(id);
        //Check if loan is not equal to null
        if(tempLoan != null)
        {
            System.out.println("Loan id: " + tempLoan.getId());
            System.out.println("Borrow date: " + tempLoan.getBorrowDate());
            System.out.println("Period: " + tempLoan.getPeriod());
            //State kan fjernes Ved ikke hvad dens betydning er. Maeske aendre den til en bool
            System.out.println("State: " + tempLoan.getState());
            System.out.println(" ");
        }
        else
        {
            System.out.println("Loan wasn't found!");
        }
    }
}
